package cn.ljy.authority.web.controller.frontend;

import cn.ljy.authority.config.security.TokenUtil;
import cn.ljy.authority.model.common.AuthorityConstant;
import cn.ljy.authority.service.SysUserService;
import cn.ljy.authority.service.SysUserTokenFrontendService;
import cn.ljy.authority.web.controller.backstage.BaseController;
import cn.ljy.common.model.result.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "前台登录模块")
@RestController
@RequestMapping(value = "/api/frontend")
public class FrontendController extends BaseController {

    @Autowired
    private SysUserTokenFrontendService sysUserTokenFrontendService;
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "PC端账户登录")
    @GetMapping("/pc/login")
    public ResponseData loginOfPc(String username,String password){
        String token=sysUserTokenFrontendService.loginAndGetToken(username,password, AuthorityConstant.CLIENT_PC);
        if(StringUtils.hasLength(token)){
            return ResponseData.success(token);
        }else{
            return ResponseData.error("登录失败");
        }
    }

    @ApiOperation(value = "移动端账户登录")
    @GetMapping("/mobile/login")
    public ResponseData loginOfmobile(String username,String password){
        String token=sysUserTokenFrontendService.loginAndGetToken(username,password, AuthorityConstant.CLIENT_MOBILE);
        if(StringUtils.hasLength(token)){
            return ResponseData.success(token);
        }else{
            return ResponseData.error("登录失败");
        }
    }

    @ApiOperation(value = "学生注销")
    @GetMapping("/logout")
    public ResponseData logout(){
        //读取token的值
        String token= TokenUtil.getAppToken();
        //移除tokeen
        sysUserTokenFrontendService.removeUserToken(token);
        return ResponseData.success("注销成功");
    }

    @ApiOperation(value = "获取当前登录账户信息")
    @GetMapping("/user/session")
    public ResponseData session(HttpServletRequest request){
        //读取token的值
        String token= TokenUtil.getAppToken();
        return ResponseData.success(sysUserTokenFrontendService.getUserFromToken(token));
    }




}
